<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <title>index</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="../../css/mui.min.css">
    <style>
		.tips{
			margin-top: 150px;
			text-align: center;
		}
    </style>
  </head>

  <body>
    <header class="mui-bar mui-bar-nav">
      <h1 class="mui-title">登录</h1>
    </header>
    <div id="testLogin" class="mui-content" style="display: none;">
      <div id="">
        <input type="" name="" id="ip" value="" style="width: 100%;" />
        <button type="button" id="changeIp">保存修改地址</button>
		<button type="button" id="clearIp">清空地址</button>
		<button type="button" id="prod">正式地址</button>
		<button type="button" id="test">测试地址</button>
	  </div>
      <input type="" name="" id="loginId" value="P628994" />
      <br>
      <button type="button" id="login">登录</button>
    </div>
	<div id="tips" class="tips" style="display: block;">loading...</div>
	
    <script src="../../js/mui.min.js"></script>
    <script src="./js/mkeyplus.base.js"></script>
    <script src="../../js/jquery-3.2.1.min.js" type="text/javascript"></script>
    <script src="./js/sqlite.js" type="text/javascript" charset="utf-8"></script>
	<script src="./js/indexDBs.js"></script>
    <script type="text/javascript" charset="utf-8">
      //mui初始化
      mui.init();
      let network;
	  let source = getUrlParams('source')
	  // 离线-----
	  // let db={};
	  // const dbName = 'myDBs'; // 数据库的名字
	  // const storeName = 'listPackage'; //仓库名称
	  // openDB(dbName, storeName, 1).then(res=>{
	  //   console.log('openDB',res)
	  //   db = res
	  // })
	  // --------
      if (window.plus) {
        plusready();
      } else {
        document.addEventListener('plusready', plusready, false);
      }
      function plusready() {
        var self = plus.webview.currentWebview();
        let ip = localStorage.getItem('serverUrl')
        $("#ip").val(ip)
      }
	  function getUrlParams(name) {
	    var  reg = new  RegExp("(^|&)" + name + "=([^&]*)(&|$)");     
	    var  r = window.location.search.substr(1).match(reg);     
	    if(r != null) return   unescape(r[2]);
	    return  null;
	  }
	  // 内部/外部人员来源
	  function inOutSourceLogin(){
		
		if(source == 'in'){
			// 内部人员
			let token = localStorage.getItem('token')
			console.log('内部人员',token)
			mui.baseApp.httpRequst('post','/app/login',{userName:token,type:2},function(res) {
			  console.log('内部',res)
			  localStorage.setItem("mqmsToken", res.data.token)
			  localStorage.setItem("mqmsUser", JSON.stringify(res.data.user))
			  mui.baseApp.openWindow('./index.html?source=in')
			})
		}else{
			// 外部人员登录
			let obj = JSON.parse(localStorage.getItem('outUser'))
			console.log('外部人员',obj.phone)
			mui.baseApp.httpRequst('post','/app/login',{userName:obj.phone,type:1},function(res) {
			  console.log('外部',res)
			  localStorage.setItem("mqmsToken", res.data.token)
			  localStorage.setItem("mqmsUser", JSON.stringify(res.data.user))
			  mui.baseApp.openWindow('./index.html?source=out')
			})
		}
	  }
	  
	  $("#changeIp").click(function() {
	    localStorage.setItem('serverUrl', $("#ip").val())
	    // mui.baseApp.serverUrl = 'http://' + $("#ip").val()
	    mui.baseApp.serverUrl = $("#ip").val()
	    mui.toast("修改成功")
	  })
      $("#clearIp").click(function() {
        $("#ip").val('')
      })
	  $("#prod").click(function() {
		$("#ip").val('https://neom.cgnpc.com.cn/smartreport-api/nemqms/')
		localStorage.setItem('serverUrl', $("#ip").val())
	  })
	  $("#test").click(function() {
	  	$("#ip").val('https://neom-t.cgnpc.com.cn/smartreport-api/ding/nemqms/index/')
		localStorage.setItem('serverUrl', $("#ip").val())
	  })
      $("#login").click(function() {
		
        let form = {
          userName: $("#loginId").val() 
        }
        mui.baseApp.httpRequst('post','/app/login',form,function(res) {
          console.log(res)
          localStorage.setItem("mqmsToken", res.data.token)
          localStorage.setItem("mqmsUser", JSON.stringify(res.data.user))
		  // 重新登录删除缓存数据
		  // cursorGetData(db, storeName).then(res=>{
		  //   console.log('获取缓存',res)
		  //   res.forEach((item,index) => {
		  //     console.log(item.id)
		  //     deleteDB(db, storeName, item.id)
		  //   })
		  // })
		  mui.baseApp.openWindow('./index.html')
        })
      })
	  // 通过NEIOS登录用token/phone获取检修token
	  if(source !== null){
		setTimeout(()=>{
			inOutSourceLogin()
		},500)
	  }else{
		  document.getElementById('testLogin').style.display = 'block'
		  document.getElementById('tips').style.display = 'none'
	  }
    </script>
  </body>

</html>
